package org.example.state;

import org.example.model.FarmMachine;
import org.example.strategy.ObstacleAvoidanceStrategy;
import org.example.strategy.PathPlanningContext;

public class AvoidingState implements MachineState {
    private PathPlanningContext pathPlanningContext;

    public AvoidingState() {
        pathPlanningContext = new PathPlanningContext();
        pathPlanningContext.setStrategy(new ObstacleAvoidanceStrategy());
    }

    @Override
    public String getStateName() {
        return "避障状态";
    }

    @Override
    public void handleEvent(FarmMachine machine, String event) {
        switch (event) {
            case "障碍物已避开":
                machine.setState(new WorkingState());
                System.out.println(machine.getType() + "[" + machine.getId() + "] 从避障状态切换到工作状态");
                break;
            case "检测到故障":
                machine.setState(new FaultState());
                System.out.println(machine.getType() + "[" + machine.getId() + "] 从避障状态切换到故障状态");
                break;
        }
    }

    @Override
    public void execute(FarmMachine machine) {
        System.out.println(machine.getType() + "[" + machine.getId() + "] 正在执行避障操作");
        pathPlanningContext.planPath(machine);

        double fuel = machine.getFuel();
        fuel -= 2.0;
        machine.setFuel(fuel);
        System.out.println(machine.getType() + "[" + machine.getId() + "] 剩余燃油: " + fuel + "%");
    }
}